local jiumeng_shunyi = fk.CreateSkill {

  name = "jiumeng_shunyi",

  tags = {  },

}

local U = require "packages/utility/utility"

jiumeng_shunyi:addEffect("active", {
  name = "jiumeng_shunyi",
  anim_type = "special",
  prompt = "#jiumeng_shunyi_prompt",
  can_use = function (self, player)
    return player:usedSkillTimes(jiumeng_shunyi.name) == 0
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = effect.from
    local judge = {
      who = player,
      reason = jiumeng_shunyi.name,
      pattern = ".",
    }
    room:judge(judge)
    if judge.card.color == Card.Black then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = jiumeng_shunyi.name,
      }
      local toId = room:askToChoosePlayers(player,{ targets = room.alive_players, min_num = 1, max_num = 1, prompt = "#jiumeng_shunyi_black", skill_name = jiumeng_shunyi.name,  cancelable = false, no_indicate = false})
      local to = toId[1]
      local num = math.max(2,to:getLostHp())
      room:damage{
        to = to,
        damage = num,
        from = player,
        damageType = fk.ThunderDamage,
        skillName = jiumeng_shunyi.name,
      }
    elseif judge.card.color == Card.Red then
      local choices = {"jiumeng_shunyi_exchange","jiumeng_shunyi_trick","jiumeng_shunyi_draw"}
      local num = 1
      if player:usedSkillTimes("jiumeng_bianshen_active",Player.HistoryGame) == 1 then
        num = 2
      end
      local choice = room:askToChoices(player,{ choices = choices, min_num = num, max_num = num, skill_name = jiumeng_shunyi.name, prompt = "#jiumeng_shunyi_choose:::"..num, cancelable = false, detailed = false, all_choices= choices})
      if table.contains(choice,"jiumeng_shunyi_exchange") then
        local toId = room:askToChoosePlayers(player,{ targets = room:getOtherPlayers(player), min_num = 1, max_num = 1, prompt = "#jiumeng_shunyi_exchangechoose", skill_name = jiumeng_shunyi.name, cancelable = false, no_indicate = false})
        local to = toId[1]
        room:swapAllCards(player, {player, to}, jiumeng_shunyi.name,"h")
      end
      if table.contains(choice,"jiumeng_shunyi_trick") then
        room:addPlayerMark(player,"@jiumeng_shunyi_trick",1)
      end
      if table.contains(choice,"jiumeng_shunyi_draw") then
        player:drawCards(4,jiumeng_shunyi.name)
      end
    end
  end
})

jiumeng_shunyi:addEffect(fk.TurnEnd, {
  refresh_events = {fk.TurnEnd},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill("jiumeng_shunyi") and player:getMark("@jiumeng_shunyi_trick") > 0 and target and target ~= player
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player,"@jiumeng_shunyi_trick",0)
  end,
})

jiumeng_shunyi:addEffect("prohibit", {
  name = "#jiumeng_shunyi_pro",
  is_prohibited = function (self, from, to, card)
    return to:hasSkill("jiumeng_shunyi") and to:getMark("@jiumeng_shunyi_trick") > 0 and to:usedSkillTimes("jiumeng_shunyi") == 0 and card.type == Card.TypeTrick
  end,
})

return jiumeng_shunyi